package com.example.crmweb.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.crmweb.entity.Dictionary;
import com.example.crmweb.mapper.DictionaryMapper;
import com.example.crmweb.service.DictionaryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author -CRM-
 * @since 2022-03-17
 */
@Service
public class DictionaryServiceImpl extends ServiceImpl<DictionaryMapper, Dictionary> implements DictionaryService {

    @Override
    public PageInfo<Dictionary> batchDescribeByPage(Integer pageSize,Integer pageNum,Dictionary dictionary) {
        return PageHelper.startPage(pageNum,pageSize).doSelectPageInfo(()->{
                baseMapper.selectList(Wrappers.query());
        });
    }

    @Override
    public List<Dictionary> batchDescribeByType(String type) {
        return baseMapper.selectList(Wrappers.<Dictionary>query()
                .eq("type",type)
                .orderByAsc("sort"));
    }

    @Override
    public List<Dictionary> batchDescribeByStatus(String status) {
        return baseMapper.selectList(Wrappers.<Dictionary>query()
                .eq("status",status));
    }

    @Override
    public List<Dictionary> batchDescribeByTypeAndAccess(String type) {
        return baseMapper.selectList(Wrappers.<Dictionary>query()
                .eq("type",type)
                .eq("status","access")
                .orderByAsc("sort"));
    }

    @Override
    public List<Dictionary> batchDescribeByTypeAndDisable(String type) {
        return baseMapper.selectList(Wrappers.<Dictionary>query()
                .eq("type",type)
                .eq("status","disable")
                .orderByAsc("sort"));
    }

    @Override
    public Optional<Dictionary> describeById(Integer id) {
        Dictionary dictionary = baseMapper.selectById(id);
        return Optional.of(dictionary);
    }

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    @Override
    public Integer create(Dictionary dictionary) {
        return baseMapper.insert(dictionary);
    }

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    @Override
    public Integer modify(Dictionary dictionary) {
        return baseMapper.updateById(dictionary);
    }

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    @Override
    public Integer delete(Integer id) {
        Optional<Dictionary> optionalDictionary = describeById(id);
        Dictionary dictionary = optionalDictionary.get();
        if ("disable".equals(dictionary.getStatus())){
            throw new NullPointerException();
        }else {
            dictionary.setStatus("disable");
            return baseMapper.updateById(dictionary);
        }
    }

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    @Override
    public boolean batchDelete(List<Integer> list) {
        for (Integer integer : list) {
            delete(integer);
        }
        return true;
    }

}
